﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using FileHelpers;

namespace Docsbox.DataMigration
{
    [DelimitedRecord(",")]
    public class Docsbox
    {
        //path to the file location
        public string FilePath;

        //not used
        public string AmendAssertID;

        //Account Comment
        public string Comments;

        //first name
        public string FirstName;

        //audit comment
        public string GeneralComments;

        //account number /mtf number
        public string MTFNo;

        //registrationnumber
        public string RegNo;

        //account status
        public string Status;

        //surname
        public string Surname;

        //not used
        public string CategoryID;

        //same as surname
        public string Client;

        //no idea, not used
        public string display_name;


        //affordability
        public string field10Approve;

        public string field10Comment;

        //deposit required
        public string field11Approve;

        public string field11Comment;

        //driver license
        public string field1Approve;

        public string field1Comment;

        //bank statement
        public string field2Approve;

        public string field2Comment;

        //confirmation of address
        public string field3Approve;

        public string field3Comment;

        //wages slip
        public string field4Approve;

        public string field4Comment;

        //visa/residency
        public string field5Approve;

        public string field5Comment;

        //credit check
        public string field6Approve;

        public string field6Comment;

        //insurance
        public string field7Approve;

        public string field7Comment;

        //risk protection wavier
        public string field8Approve;

        public string field8Comment;

        //cdp cover
        public string field9Approve;

        public string field9Comment;

        //File name only with file extension
        public string meta_fileName;

        //same as comment
        public string Notes;


        //office ID
        public string officeID;

        //email of completed person
        public string Person_Completed_Email;

        //completed person name
        public string Person_Completed_Name;
        //not used
        public string person_Amend;

        public string person_Amend_Name;

        public string Version_Amend;

        public string file_text;

        public string Campaign_name;

        public string custom_Field1;

        public string custom_Field2;

        public string custom_Field3;

        public string custom_Field4;

        public string job_description;

        public string keywords;

        public string price_text;

        public string product_code;

        public bool IsValidRecord()
        {
            bool successMtfNo = ValidateMtfNo();
            if (!successMtfNo) return false;
            bool validPdfFile = ValidatePdfFile();
            if (!validPdfFile) return false;
            bool validateOffice = ValidateOffice();
            if (!validateOffice) return false;

            return true;
        }

        private bool ValidateOffice()
        {
            if (string.IsNullOrEmpty(officeID))
            {
                _errorCode = ErrorCode.MissingOffice;
                return false;
            }
            int id;

            if(Int32.TryParse(officeID, out id))
            {
                if (id > 35)
                {
                    _errorCode = ErrorCode.TestData;
                    return false;
                }
                return true;
            }
            _errorCode = ErrorCode.InvalidOffice;
            return false;
        }

        private bool ValidatePdfFile()
        {
            if(string.IsNullOrEmpty(FilePath.Trim()))
            {
                _errorCode = ErrorCode.NoPdfPath;
                return false;
            }
            if (File.Exists(FilePath))
            {
                if(string.IsNullOrEmpty(meta_fileName.Trim()))
                    meta_fileName = FilePath.Substring(FilePath.LastIndexOf("\\", StringComparison.OrdinalIgnoreCase) + 1);
                return true;
            }
            _errorCode = ErrorCode.MissingPdf;
            return false;
        }

        [FieldIgnored]
        private ErrorCode _errorCode;
        public ErrorCode Error()
        {
            return _errorCode;
        }

        private bool ValidateMtfNo()
        {
                        
            MTFNo = MTFNo.Trim().Trim('.');
            if (string.IsNullOrEmpty(MTFNo))
            {
                _errorCode = ErrorCode.MissingMtfNo;
                return false;
            }
            if (Regex.IsMatch(MTFNo, @"^MTF\d+$"))
                return true;
            if(Regex.IsMatch(MTFNo, @"^\d+$"))
            {
                MTFNo = "MTF" + MTFNo;
                return true;
            }
            Regex regex = new Regex(@"^MTF (\d+)$");
            Match match = regex.Match(MTFNo);

            if(match.Success)
            {
                string mtfDigit = match.Groups[1].Value;
                MTFNo = "MTF" + mtfDigit;
                return true;
            }
            _errorCode = ErrorCode.InvalidMtfNo;
            return false;
        }


        public static List<string> CsvHeaders()
        {
            return new List<string> { "MTFNo","FirstName","Surname","RegNo", "Status","Client", "officeID", "FilePath", "Error" };
        } 
        public List<string> ToCsvErrorList()
        {
            return new List<string> { MTFNo, FirstName, Surname, RegNo, Status, Client, officeID, FilePath, Error().ToString() };
        }
    }


    public enum ErrorCode
    {
        InvalidMtfNo,
        MissingMtfNo,
        MissingPdf,
        NoPdfPath,
        MissingOffice,
        InvalidOffice,
        TestData
    }

    [DelimitedRecord(",")]
    public class DocsboxDates
    {
        public string project;

        public string review_date;

        public string created;


        public bool ValidateMtfNo()
        {

            project = project.Trim().Trim('.');
            if (string.IsNullOrEmpty(project))
            {
                return false;
            }
            if (Regex.IsMatch(project, @"^MTF\d+$"))
                return true;
            if (Regex.IsMatch(project, @"^\d+$"))
            {
                project = "MTF" + project;
                return true;
            }
            Regex regex = new Regex(@"^MTF (\d+)$");
            Match match = regex.Match(project);

            if (match.Success)
            {
                string mtfDigit = match.Groups[1].Value;
                project = "MTF" + mtfDigit;
                return true;
            }
            return false;
        }
    }
}
